import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.OtherContent;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.util.ContentType;
import com.sun.javaws.exceptions.ExitException;
import sample.photos.PicasawebClient;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.List;

public class UploadDialog extends JDialog {
    private boolean result = true;
    JTextField loginField;
    JLabel loginLabel;
    JPasswordField passwdField;
    JLabel passwdLabel;
    //    JTextField urlField;
    //    JLabel urlLabel;
    JLabel albumsNames;
    JComboBox existingAlbums;
    JTextField albumNameField;
    JLabel albumNameLabel;
    JButton uploadButton;
    JButton updateButton;
    PicasawebClient client;
    String outputFile;
    AlbumEntry album;
    List<AlbumEntry> listAlbums;

    public void initComponents() {

        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
        this.setContentPane(mainPanel);
        JPanel inMainPanel = new JPanel(new GridLayout(5, 2, 7, 12));
        inMainPanel.setBackground(Color.GRAY);
        loginLabel = new JLabel(" Login  ");
        passwdLabel = new JLabel(" Password");
        loginField = new JTextField("NadyaZabr", 17);
        passwdField = new JPasswordField("", 15);
        albumsNames = new JLabel("albums");
        existingAlbums = new JComboBox();
        albumNameField = new JTextField("", 15);
        albumNameLabel = new JLabel(" Name of album");

        inMainPanel.add(loginLabel);
        inMainPanel.add(loginField);
        inMainPanel.add(passwdLabel);
        inMainPanel.add(passwdField);
        inMainPanel.add(albumsNames);
        inMainPanel.add(existingAlbums);
        inMainPanel.add(albumNameLabel);
        inMainPanel.add(albumNameField);

        updateButton = new JButton(" Update ");
        updateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                existingAlbums.removeAllItems();
                PicasawebService service = new PicasawebService("exampleClient");
                client = new PicasawebClient(service, loginField.getText(), new String(passwdField.getPassword()));
                try {
                    listAlbums = client.getAlbums(loginField.getText());
                    for (AlbumEntry entry : listAlbums) {
                        existingAlbums.addItem(entry.getTitle().getPlainText());
                    }
                    repaint();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }


//                result = false;
//                dispose();
            }
        });
        uploadButton = new JButton(" Upload ");
        uploadButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                result = true;
                PicasawebService service = new PicasawebService("exampleClient");
                client = new PicasawebClient(service, loginField.getText(), new String(passwdField.getPassword()));
                if (albumNameField.getText().equals("") && listAlbums!=null) {
                try {
                     album = listAlbums.get(existingAlbums.getSelectedIndex());
                    createPhotos(album);
                    
                    } catch (ExitException ee) {
                        return;
                    } catch (Exception e1) {
                    e1.printStackTrace();

                }


                } else {
                    try {
                        createAlbum();
                        createPhotos(album);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }

            }
        });
        inMainPanel.add(updateButton);
        inMainPanel.add(uploadButton);
        mainPanel.add(inMainPanel);
    }

    public boolean exec() {
        setSize(new Dimension(300, 250));
        this.setVisible(true);
        return result;
    }

    public UploadDialog(Frame owner, String fileName) {
        super(owner, false);
        outputFile = fileName;
        setTitle("Uploader.");
        this.setLocation(300, 300);
        setPreferredSize(new Dimension(300, 250));
        initComponents();
        getContentPane().setBackground(Color.GRAY);
        setResizable(false);
        exec();


    }


    private void createAlbum() throws Exception {
        album = new AlbumEntry();

        String title = albumNameField.getText();//getString("Title");
        album.setTitle(new PlainTextConstruct(title));
        String description = "empty";//getString("Description");
        album.setDescription(new PlainTextConstruct(description));

        String access = "public"; // "";
        album.setAccess(access);
        String location = "http://picasaweb.google.com/";//urlField.getText();//getString("Location");
        album.setLocation(location);
        album.setDate(new Date());

        album = client.insertAlbum(album);
    }

    private void createPhotos(AlbumEntry albumEntry) throws Exception {
//        File input = new File(outputFile);
        File input = new File("tmpImages/");
        String[] names = input.list();
//        Scanner scan = null;


        for (String i : names) {
            String nextName = i;
            PhotoEntry photo = new PhotoEntry();

            String title = nextName;
            photo.setTitle(new PlainTextConstruct(title));
            String description = title + "something";
            photo.setDescription(new PlainTextConstruct(description));
            photo.setTimestamp(new Date());

            OtherContent content = new OtherContent();


            File file = null;
            file = new File("tmpImages/" + nextName);
            if (file.isDirectory()) {
                continue;
            }
            content.setBytes(getBytes(file));
            content.setMimeType(new ContentType("image/jpeg"));
            photo.setContent(content);

            photo = client.insert(albumEntry, photo);
        }

    }


    private byte[] getBytes(File file) throws FileNotFoundException, IOException {
        byte[] result = new byte[(int) file.length()];
        new FileInputStream(file).read(result);
        return result;
    }

}
